package net.xrkw.server.module.client.controller.admin.softinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import net.xrkw.server.framework.common.pojo.PageParam;
import net.xrkw.server.framework.common.pojo.PageResult;
import net.xrkw.server.framework.common.pojo.CommonResult;
import net.xrkw.server.framework.common.util.object.BeanUtils;
import static net.xrkw.server.framework.common.pojo.CommonResult.success;

import net.xrkw.server.framework.excel.core.util.ExcelUtils;

import net.xrkw.server.framework.apilog.core.annotation.ApiAccessLog;
import static net.xrkw.server.framework.apilog.core.enums.OperateTypeEnum.*;

import net.xrkw.server.module.client.controller.admin.softinfo.vo.*;
import net.xrkw.server.module.client.dal.dataobject.softinfo.SoftInfoDO;
import net.xrkw.server.module.client.service.softinfo.SoftInfoService;

@Tag(name = "管理后台 - 软件信息")
@RestController
@RequestMapping("/client/soft-info")
@Validated
public class SoftInfoController {

    @Resource
    private SoftInfoService softInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建软件信息")
    @PreAuthorize("@ss.hasPermission('client:soft-info:create')")
    public CommonResult<Long> createSoftInfo(@Valid @RequestBody SoftInfoSaveReqVO createReqVO) {
        return success(softInfoService.createSoftInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新软件信息")
    @PreAuthorize("@ss.hasPermission('client:soft-info:update')")
    public CommonResult<Boolean> updateSoftInfo(@Valid @RequestBody SoftInfoSaveReqVO updateReqVO) {
        softInfoService.updateSoftInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除软件信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('client:soft-info:delete')")
    public CommonResult<Boolean> deleteSoftInfo(@RequestParam("id") Long id) {
        softInfoService.deleteSoftInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除软件信息")
                @PreAuthorize("@ss.hasPermission('client:soft-info:delete')")
    public CommonResult<Boolean> deleteSoftInfoList(@RequestParam("ids") List<Long> ids) {
        softInfoService.deleteSoftInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得软件信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('client:soft-info:query')")
    public CommonResult<SoftInfoRespVO> getSoftInfo(@RequestParam("id") Long id) {
        SoftInfoDO softInfo = softInfoService.getSoftInfo(id);
        return success(BeanUtils.toBean(softInfo, SoftInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得软件信息分页")
    @PreAuthorize("@ss.hasPermission('client:soft-info:query')")
    public CommonResult<PageResult<SoftInfoRespVO>> getSoftInfoPage(@Valid SoftInfoPageReqVO pageReqVO) {
        PageResult<SoftInfoDO> pageResult = softInfoService.getSoftInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SoftInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出软件信息 Excel")
    @PreAuthorize("@ss.hasPermission('client:soft-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSoftInfoExcel(@Valid SoftInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SoftInfoDO> list = softInfoService.getSoftInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "软件信息.xls", "数据", SoftInfoRespVO.class,
                        BeanUtils.toBean(list, SoftInfoRespVO.class));
    }

}